var ShopLayer = cc.Layer.extend({
    isMouseDown:false,

    ccbNode:null,

    rectsInited:false,

    floatItem: null,

    itemNameLabel: null,
    itemInfoLabel: null,
    itemAttrLabel: null,
    itemPriceLabel: null,

    init:function () {

        //////////////////////////////
        // 1. super init first
        this._super();
        this.setTouchEnabled(true);
        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask director the window size
        var size = cc.Director.getInstance().getVisibleSize();
        var size2 =  cc.Director.getInstance().getWinSize();

        cc.BuilderReader.setResourcePath("ccb/");

        //load ccbi scene
        this.ccbNode = cc.BuilderReader.load("ccb/BagScene.ccbi", this);
        //this.ccbNode.animationManager.setCompletedAnimationCallback(this, this.animationCompleteCallback);

        //init scene
        //read record
        /*
            back 2
            confirm 3
            bag 30
                box 1-20 
                    weapon 1
                    selectBorder 2
                coinNum 50
            shop 20
                box 1-10
                    weapon 1
                    selectBorder 2
            intro 10
                name 10
                intro 11
                attribute 12
                coinNum 13
            dragIcon 100

    
            GameUI:
            skill1 1
            skill2 2
            pause 3
            myline 4-5
                name 1
                line 2
            actionline 6
                sprite 1 ------ ccbi node

            SingleCharacter:
                character 1
                bloodline 2
                energyline 3
        */

        // 浮动元素
        this.floatItem=this.ccbNode.getChildByTag(100);
        this.floatItem.setVisible(false);

        // 物品信息
        this.itemNameLabel=this.ccbNode.getChildByTag(10).getChildByTag(10);
        this.itemInfoLabel=this.ccbNode.getChildByTag(10).getChildByTag(11);
        this.itemAttrLabel=this.ccbNode.getChildByTag(10).getChildByTag(12);
        this.itemPriceLabel=this.ccbNode.getChildByTag(10).getChildByTag(13);   
        this.itemNameLabel.setVisible(false);
        this.itemInfoLabel.setVisible(false);     
        this.itemAttrLabel.setVisible(false);
        this.itemPriceLabel.setVisible(true);
        this.itemPriceLabel.setString(0);

        // 初始化商店界面
        this.refreshBagAndShop();

        if(this.ccbNode != null) {
            this.addChild(this.ccbNode);
        }

        this.initTouchPositions();

        // test
    },
    animationCompleteCallback: function(){
        console.log("animation end...");
    },

    backBtnRect:null,
    confirmBtnRect:null,
    bagBorderRect:null,
    shopBorderRect:null,
    bagRects:[0],
    shopRects:[0],
    initTouchPositions: function(){
        if(!this.rectsInited){
            //init rects
            this.backBtnRect=this.ccbNode.getChildByTag(2).getBoundingBox();
            this.confirmBtnRect=this.ccbNode.getChildByTag(3).getBoundingBox();

            // 20格背包
            this.bagBorderRect=this.ccbNode.getChildByTag(30).getBoundingBox();
            for(var i=1;i<=20;i++){
                var rect=this.ccbNode.getChildByTag(30).getChildByTag(i).getBoundingBox();
                this.bagRects.push(rect);
            }

            // 10格商店
            this.shopBorderRect=this.ccbNode.getChildByTag(20).getBoundingBox();
            for(var j=1;j<=10;j++){
                var rect=this.ccbNode.getChildByTag(20).getChildByTag(j).getBoundingBox();
                this.shopRects.push(rect);
            }

            this.rectsInited=true;
        }
    },

    touchedBagOrShop:0, // 1 bag 2 shop  点击的是商店或是背包
    touchedItemType:0, // 1-6 点击的物品种类
    touchedItemId:0, // 1-5 点击的物品id
    touchedItemIndex:0, // 1-20 or 1-10 点击的物品index
    onTouchesEnded: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
        // console.log("touch end",touchPoint);

        // touch on back
        if(cc.rectContainsPoint(this.backBtnRect, touchPoint)){
            console.log("Touch BACK");
            Game.currScene=new MainStageScene();
            cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
            return true;
        }
        // touch on confirm
        if(cc.rectContainsPoint(this.confirmBtnRect, touchPoint)){
            console.log("Touch Confirm");
            Game.currScene=new MainStageScene();
            cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
            return true;
        }

        this.floatItem.setVisible(false);

        // touch on bag item
        var touchAtBag=this.ccbNode.getChildByTag(30).convertToNodeSpace(touchPoint);
        if(cc.rectContainsPoint(this.bagBorderRect, touchPoint)){
            for(var i=1;i<=20;i++){
                if(cc.rectContainsPoint(this.bagRects[i], touchAtBag)){
                    console.log("Touch end bag item "+i);
                }
            }
            if(this.touchedBagOrShop==2){
                // 从商店拖到背包
                // 背包添加
                Game.bagAddItem(Game.currRecord, this.touchedItemType, this.touchedItemId);
                // 商店删除
                Game.shopDelItem(Game.currRecord, this.touchedItemIndex);
                // 刷新
                this.refreshBagAndShop();
            }
            this.touchedBagOrShop=0;
            this.touchedItemType=0;
            this.touchedItemId=0;
            this.touchedItemIndex=0;
            return true;
        }

        // touch on shop item
        var touchAtShop=this.ccbNode.getChildByTag(20).convertToNodeSpace(touchPoint);
        if(cc.rectContainsPoint(this.shopBorderRect, touchPoint)){
            for(var i=1;i<=10;i++){
                if(cc.rectContainsPoint(this.shopRects[i], touchAtShop)){
                    console.log("Touch end shop item "+i);
                }
            }
            if(this.touchedBagOrShop==1){
                // 从背包拖到商店
                // 背包删除
                Game.bagDelItem(Game.currRecord, this.touchedItemIndex);
                // 刷新
                this.refreshBagAndShop();
            }
            this.touchedBagOrShop=0;
            this.touchedItemType=0;
            this.touchedItemId=0;
            this.touchedItemIndex=0;
            return true;
        }

        this.touchedBagOrShop=0;
        this.touchedItemType=0;
        this.touchedItemId=0;
        this.touchedItemIndex=0;
    },
    onTouchesBegan: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();

        // touch on bag item
        var touchAtBag=this.ccbNode.getChildByTag(30).convertToNodeSpace(touchPoint);
        for(var i=1;i<=20;i++){
            if(cc.rectContainsPoint(this.bagRects[i], touchAtBag)){
                console.log("Touch begin bag item "+i);
                var equipItem = Game.gameRecord[Game.currRecord]["have_equip"][i];
                var equipImgPath = this.getEquipImgPathByTypeAndId(equipItem[0],equipItem[1]);
                var texture = cc.TextureCache.getInstance().addImage(equipImgPath);
                this.floatItem.setTexture(texture);
                this.setEquipInfo(equipItem[0],equipItem[1]);
                this.showItemSelectBorder(30,i);

                this.touchedBagOrShop=1;
                this.touchedItemType=equipItem[0];
                this.touchedItemId=equipItem[1];
                this.touchedItemIndex=i;
                return true;
            }
        }

        // touch on shop item
        var touchAtBag=this.ccbNode.getChildByTag(20).convertToNodeSpace(touchPoint);
        for(var i=1;i<=10;i++){
            if(cc.rectContainsPoint(this.shopRects[i], touchAtBag)){
                console.log("Touch begin shop item "+i);
                var equipImgPath = this.getEquipImgPathByTypeAndId(Game.gameShopItems[i][0],Game.gameShopItems[i][1]);
                var texture = cc.TextureCache.getInstance().addImage(equipImgPath);
                this.floatItem.setTexture(texture);
                this.setEquipInfo(Game.gameShopItems[i][0],Game.gameShopItems[i][1]);
                this.showItemSelectBorder(20,i);

                this.touchedBagOrShop=2;
                this.touchedItemType=Game.gameShopItems[i][0];
                this.touchedItemId=Game.gameShopItems[i][1];
                this.touchedItemIndex=i;
                return true;
            }
        }

        // touch begin on other
        this.cancelItemSelectBorder();

    },
    onTouchesMoved: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();

        if(this.touchedItemId==0 && this.touchedItemType==0){

        }else{
            this.floatItem.setVisible(true);
            this.floatItem.setPosition(touchPoint);
        }
    },

    // 根据type和id获得物品路径
    getEquipImgPathByTypeAndId: function(type,id){
        var equipImgPath="equips/";
        switch(type){
            case 1:
                equipImgPath=equipImgPath+"weapon/"+id+".png";
                break;
            case 2:
                equipImgPath=equipImgPath+"head/"+id+".png";
                break;
            case 3:
                equipImgPath=equipImgPath+"armor/"+id+".png";
                break;
            case 4:
                equipImgPath=equipImgPath+"hand/"+id+".png";
                break;
            case 5:
                equipImgPath=equipImgPath+"boots/"+id+".png";
                break;
            case 6:
                equipImgPath=equipImgPath+"ring/"+id+".png";
                break;
        }
        return equipImgPath;
    },
    setEquipInfo: function(type,id){
        if(type==0 && id==0){
            this.clearEquipInfo();
        }else{
            this.itemNameLabel.setString(Game.equipInfo[type][id]["name"]);
            this.itemInfoLabel.setString(Game.equipInfo[type][id]["intro"]);
            this.itemAttrLabel.setString(Game.equipInfo[type][id]["attr"]);
            this.itemPriceLabel.setString(Game.equipInfo[type][id]["price"]);
            this.itemNameLabel.setVisible(true);
            this.itemInfoLabel.setVisible(true);     
            this.itemAttrLabel.setVisible(true);
            this.itemPriceLabel.setVisible(true);
        }
    },
    clearEquipInfo: function(){
        this.itemPriceLabel.setString(0);
        this.itemNameLabel.setVisible(false);
        this.itemInfoLabel.setVisible(false);     
        this.itemAttrLabel.setVisible(false);
        this.itemPriceLabel.setVisible(true);
    },
    showItemSelectBorder: function(type,index){
        if(type==30){
            // 是背包 商店物品全取消选中
            for(var i=1;i<=10;i++){
                this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setVisible(false);
                this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setOpacity(0);
            }
            for(var i=1;i<=20;i++){
                if(index==i){
                    this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setVisible(true);
                    this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setOpacity(255);
                }else{
                    this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setVisible(false);
                    this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setOpacity(0);
                }
            }
        }
        if(type==20){
            // 是商店 背包物品全取消选中
            for(var i=1;i<=20;i++){
                this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setVisible(false);
                this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setOpacity(0);
            }
            for(var i=1;i<=10;i++){
                if(index==i){
                    this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setVisible(true);
                    this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setOpacity(255);
                }else{
                    this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setVisible(false);
                    this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setOpacity(0);
                }
            }
        }
    },
    cancelItemSelectBorder: function(){
        for(var i=1;i<=20;i++){
            this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setVisible(false);
            this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(2).setOpacity(0);
        }
        for(var i=1;i<=10;i++){
            this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setVisible(false);
            this.ccbNode.getChildByTag(20).getChildByTag(i).getChildByTag(2).setOpacity(0);
        }
        this.clearEquipInfo();
    },
    refreshBagAndShop: function(){
        for(var i=1;i<=20;i++){
            var equipItem = Game.gameRecord[Game.currRecord]["have_equip"][i];
            if(equipItem[0]==0){
                this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(1).setVisible(false);
            }else{
                var imgPath = this.getEquipImgPathByTypeAndId(equipItem[0],equipItem[1]);
                var texture = cc.TextureCache.getInstance().addImage(imgPath);
                this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(1).setTexture(texture);
                this.ccbNode.getChildByTag(30).getChildByTag(i).getChildByTag(1).setVisible(true);
            }
        }
        
        for(var j=1;j<=10;j++){
            var type=Game.gameShopItems[j][0];
            var equipId=Game.gameShopItems[j][1];
            if(type==0){
                this.ccbNode.getChildByTag(20).getChildByTag(j).getChildByTag(1).setVisible(false);
            }else{
                var equipImgPath = this.getEquipImgPathByTypeAndId(type,equipId);
                var texture = cc.TextureCache.getInstance().addImage(equipImgPath);
                this.ccbNode.getChildByTag(20).getChildByTag(j).getChildByTag(1).setTexture(texture);
                this.ccbNode.getChildByTag(20).getChildByTag(j).getChildByTag(1).setVisible(true);
            }
        }
                
        this.cancelItemSelectBorder();
    }

});

var ShopScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new ShopLayer();
        layer.init();
        this.addChild(layer);
    }
});